home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Snippets / Devices / SCSI Simple Sample / Src / WindowUtilities.c < prev   
Encoding:
C/C++ Source or Header  |  1994-03-24  |  5.1 KB  |  205 lines  |  [TEXT/KAHL]

  1. /*                                WindowUtilities.c                                */
  2. /*
  3.  * WindowUtilities.c
  4.  * Copyright © 1993 Apple Computer Inc. All rights reserved.
  5.  * These functions are called when the window is zoomed or resized. They do
  6.  * not require any knowledge of the application itself.
  7.  */
  8. #ifndef THINK_C                /* MPW includes            */
  9. #include <Errors.h>
  10. #include <Script.h>
  11. #include <Types.h>
  12. #include <Resources.h>
  13. #include <QuickDraw.h>
  14. #include <Fonts.h>
  15. #include <Events.h>
  16. #include <Windows.h>
  17. #include <ToolUtils.h>
  18. #include <Memory.h>
  19. #include <Menus.h>
  20. #include <Lists.h>
  21. #include <Printing.h>
  22. #include <Dialogs.h>
  23. #endif
  24. #pragma segment MainCode
  25.  
  26. #ifndef TRUE
  27. #define TRUE    1
  28. #define FALSE    0
  29. #endif
  30.  
  31. void                                DoZoomWindow(
  32.         WindowPtr                        theWindow,
  33.         short                            whichPart
  34.     );
  35. Boolean                                DoGrowWindow(
  36.         WindowPtr                        theWindow,
  37.         Point                            eventWhere,
  38.         short                            minimumWidth,
  39.         short                            minimumHeight
  40.     );
  41. void                                MyDrawGrowIcon(
  42.         WindowPtr                        theWindow
  43.     );
  44.  
  45. #include <GestaltEqu.h>
  46.  
  47. #ifndef topLeft
  48. #define topLeft(r)    (((Point *) &(r))[0])
  49. #define botRight(r)    (((Point *) &(r))[1])
  50. #endif
  51. #ifndef width
  52. #define width(r)    ((r).right - (r).left)
  53. #define height(r)    ((r).bottom - (r).top)
  54. #endif
  55. #define kScrollBarWidth            (16)
  56. #define kScrollBarOffset        (kScrollBarWidth - 1)
  57.  
  58. /*
  59.  * DoZoomWindow
  60.  * Algorithm from New Inside Mac (Toolbox Essentials) 4-55
  61.  */
  62. void
  63. DoZoomWindow(
  64.         WindowPtr                        theWindow,
  65.         short                            whichPart
  66.     )
  67. {
  68.         GDHandle                gd;
  69.         GDHandle                gdZoom;
  70.         GrafPtr                    savePort;
  71.         Rect                    windowRect;
  72.         Rect                    zoomRect;
  73.         Rect                    intersection;
  74.         long                    thisArea;
  75.         long                    greatestArea;
  76.         short                    windowTitleHeight;
  77.         long                    response;
  78. #define PEEK    (*((WindowPeek) theWindow))
  79. #define STATE    (**((WStateDataHandle) PEEK.dataHandle))
  80.  
  81.         GetPort(&savePort);
  82.         SetPort(theWindow);
  83.         if (whichPart == inZoomOut) {
  84.             if (Gestalt(gestaltQuickdrawVersion, &response) != noErr
  85.               || response < gestalt8BitQD) {
  86.                 /*
  87.                  * This shouldn't happen on a modern system, but,
  88.                  * if it does, just force a single screen zoom.
  89.                  */
  90.                 zoomRect = qd.screenBits.bounds;
  91.                 InsetRect(&zoomRect, 4, 4);
  92.                 STATE.stdState = zoomRect;
  93.             }
  94.             else {
  95.                 /*
  96.                  * We have color QuickDraw. Locate the screen that contains
  97.                  * the largest area of the window and zoom to that screen.
  98.                  */
  99.                 windowRect = theWindow->portRect;
  100.                 LocalToGlobal(&topLeft(windowRect));
  101.                 LocalToGlobal(&botRight(windowRect));
  102.                 windowTitleHeight = windowRect.top
  103.                         - 1
  104.                         - (**PEEK.strucRgn).rgnBBox.top;
  105.                 windowRect.top -= windowTitleHeight;
  106.                 greatestArea = 0;
  107.                 gdZoom = NULL;
  108.                 /*
  109.                  * Look at all graphics devices and find an intersection. Then
  110.                  * select the largest intersecting screen.
  111.                  */
  112.                 for (gd = GetDeviceList(); gd != NULL; gd = GetNextDevice(gd)) {
  113.                     if (TestDeviceAttribute(gd, screenDevice)
  114.                      && TestDeviceAttribute(gd, screenActive)
  115.                      && SectRect(&windowRect, &(**gd).gdRect, &intersection)) {
  116.                         thisArea = ((long) width(intersection))
  117.                                  * ((long) height(intersection));
  118.                         if (thisArea > greatestArea) {
  119.                             greatestArea = thisArea;
  120.                             gdZoom = gd;
  121.                         }
  122.                     }
  123.                 }
  124.                 /*
  125.                  * If we're zooming to the device with the menu bar,
  126.                  * allow for its height.
  127.                  */
  128.                 if (GetMainDevice() == gdZoom)
  129.                     windowTitleHeight += GetMBarHeight();
  130.                 zoomRect = (**gdZoom).gdRect;
  131.                 InsetRect(&zoomRect, 3, 3);
  132.                 zoomRect.top += windowTitleHeight;
  133.                 STATE.stdState = zoomRect;
  134.             }                                        /* End if color QuickDraw    */
  135.         }                                            /* End if zoom out            */
  136.         ZoomWindow(theWindow, whichPart, (theWindow == FrontWindow()));
  137.         /*
  138.          * Zoom redraws the entire window.
  139.          */
  140.         EraseRect(&theWindow->portRect);
  141.         InvalRect(&theWindow->portRect);
  142.         SetPort(savePort);            
  143. #undef PEEK
  144. #undef STATE
  145. }
  146.  
  147. /*
  148.  * DoGrowWindow
  149.  * Algorithm from New Inside Mac (Toolbox Essentials) 4-58
  150.  * We assume that the window can cover the entire screen.
  151.  */
  152. Boolean
  153. DoGrowWindow(
  154.         WindowPtr                        theWindow,
  155.         Point                            eventWhere,
  156.         short                            minimumWidth,
  157.         short                            minimumHeight
  158.     )
  159. {
  160.         long                    growSize;
  161.         Rect                    limitRect;
  162.         
  163.         limitRect.left = minimumWidth;
  164.         limitRect.top = minimumHeight;
  165.         limitRect.right = qd.screenBits.bounds.right;
  166.         limitRect.bottom = qd.screenBits.bounds.bottom;
  167.         growSize = GrowWindow(theWindow, eventWhere, &limitRect);
  168.         if (growSize != 0) {
  169.             SizeWindow(theWindow, LoWord(growSize), HiWord(growSize), TRUE);
  170.             /*
  171.              * Force a redraw of the entire window, but exclude the
  172.              * intersection of the old view rect and the new view rect.
  173.              * Invalidate any prior update region in any case.
  174.              */
  175.             EraseRect(&theWindow->portRect);
  176.             InvalRect(&theWindow->portRect);
  177.         }
  178.         return (growSize != 0);
  179. }
  180.  
  181.  
  182. /*
  183.  * Draw the grow box, but don't draw the scroll bar lines.
  184.  */
  185. void
  186. MyDrawGrowIcon(
  187.         WindowPtr                        theWindow
  188.     )
  189. {
  190.         RgnHandle            clipRgn;
  191.         Rect                clipRect;
  192.         
  193.         clipRgn = NewRgn();
  194.         GetClip(clipRgn);
  195.         clipRect = theWindow->portRect;
  196.         clipRect.top = clipRect.bottom - kScrollBarOffset;
  197.         clipRect.left = clipRect.right - kScrollBarOffset;
  198.         ClipRect(&clipRect);
  199.         DrawGrowIcon(theWindow);
  200.         ValidRect(&clipRect);
  201.         SetClip(clipRgn);
  202.         DisposeRgn(clipRgn);
  203. }
  204.  
  205.